package com.teamclub.erp.sservices

import io.ebean.{Ebean, ExpressionList}
import com.teamclub.domain.erp.product.ProductSpecs
import com.teamclub.erp.sforms.ProductSpecsForm
import org.apache.commons.lang3.StringUtils
import org.springframework.stereotype.Service
import java.util.Map

import com.google.common.collect.Maps
import com.teamclub.domain.erp.dealer.DealerInfoLink
import com.teamclub.util.errors.ErrorCode
import com.teamclub.util.libs.F
import com.teamclub.util.page.Pager
import org.springframework.beans.BeanUtils

import scala.collection.JavaConversions._

/**
  * Created by ilkkzm on 17-9-12.
  */
@Service
class ProductSpecsService {
  def expr(where: ExpressionList[ProductSpecs], form: ProductSpecsForm): Unit = {
    if(StringUtils.isNotBlank(form.organNo)){
      where.eq("organNo", form.organNo)
    }
    if(form.productId != null) {
      where.eq("productId", form.productId)
    }
    if(form.brandId != null) {
      where.eq("brandId", form.brandId)
    }
    if(StringUtils.isNotBlank(form.specsName)) {
      where.like("specsName", "%" + form.specsName + "%")
    }
    where.orderBy("createdTime desc")
  }

  def listDealerProductList(form: ProductSpecsForm): F.Either[Map[String, Map[String, String]], ErrorCode] = {
    if(StringUtils.isBlank(form.organNo)){
      return F.Either.Right(ErrorCode.ref("需提供供应商编号"))
    }
    val where = Ebean.find(classOf[ProductSpecs]).where()
    expr(where, form)
    val list = where.findList()
    var geneDicts: Map[String, Map[String, String]] = Maps.newHashMap()
    for(single: ProductSpecs <- list) {
      var valMap: Map[String, String] = Maps.newHashMap()
      valMap.put("specName", single.getSpecsName)
      valMap.put("specPrice", single.getSpecsNum + "/" + single.getSpecsUnit)
      geneDicts.put(single.getSpecsCode, valMap)
    }
    F.Either.Left(geneDicts)
  }

  def listDict(form: ProductSpecsForm): F.Either[Map[String, Map[String, String]], ErrorCode] = {
    var where = Ebean.find(classOf[ProductSpecs]).where()
    expr(where, form)
    val list = where.findList()
    var geneDicts: Map[String, Map[String, String]] = Maps.newHashMap()
    for(single: ProductSpecs <- list) {
      var valMap: Map[String, String] = Maps.newHashMap()
      valMap.put("specName", single.getSpecsName)
      valMap.put("specPrice", single.getSpecsNum + "/" + single.getSpecsUnit)
      geneDicts.put(single.getSpecsCode, valMap)
    }
    F.Either.Left(geneDicts)
  }

  def list(form: ProductSpecsForm): F.Either[Pager[ProductSpecs], ErrorCode] = {
    var where = Ebean.find(classOf[ProductSpecs]).where()
    expr(where, form)
    F.Either.Left(Pager.genePager(where.query(), form))
  }

  def add(form: ProductSpecsForm): F.Either[ProductSpecs, ErrorCode] = {
    var instance = new ProductSpecs
    if(form.id != null) {
      instance = Ebean.find(classOf[ProductSpecs], form.id)
    }
    if(instance == null) {
      return F.Either.Right(ErrorCode.ENTITY_NOT_FOUND)
    }
    BeanUtils.copyProperties(form, instance)
    if(form.id != null) {
      instance.update()
    } else {
      instance.save()
    }
    F.Either.Left(instance)
  }

  def del(form: ProductSpecsForm): F.Either[ProductSpecs, ErrorCode] = {
    if(form.id == null) {
      return F.Either.Right(ErrorCode.BIZ_FAILURE)
    }
    val instance = Ebean.find(classOf[ProductSpecs], form.id)
    if(instance == null) {
      return F.Either.Right(ErrorCode.ENTITY_NOT_FOUND)
    }
    instance.delete()
    F.Either.Left(instance)
  }

}
